package com.caogen.cg_wl_kt.utils

import android.text.TextUtils
import java.util.regex.Pattern

/**
 * 项目名称: cg_wl_kt
 * Packages:com.caogen.cg_wl_kt.utils
 * author: FSW
 * 创建日期: 2024/2/26 09:45
 * Describe:
 *
 */
object HelperUtils {
    /**
     * 判断手机号码格式是否正确
     */
    fun isCellphone(phone: String?): Boolean {
        val pattern = Pattern.compile("1[0-9]{10}")
        val matcher = pattern.matcher(phone)
        return matcher.matches()
    }

    /**
     * 判断邮箱格式是否正确
     */
    fun isEmail(email: String?): Boolean {
        val strPattern = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"
        val p = Pattern.compile(strPattern)
        val m = p.matcher(email)
        return m.matches()
    }

    /**
     * **判断一个字符序列是否为空**
     *
     *  * 如果字符序列对象为空则返回true
     *  * 如果字符序列所有字符均为空格则返回true
     *
     *
     * @param cs 需要判断的字符序列对象
     * @return 结果为空返回true，否则为false
     */
    fun stringIsBlank(cs: CharSequence?): Boolean {
        if (cs == null) {
            return true
        }
        val strlen = cs.length
        if (strlen == 0) {
            return true
        }
        if ("null" == cs) {
            return true
        }
        for (i in 0 until strlen) {
            if (!Character.isWhitespace(cs[i])) {
                return false
            }
        }
        return true
    }

    /**
     * 解码UniCode
     */
    fun unescapeUnicode(str: String?): String? {
        val sb = StringBuffer()
        val matcher = Pattern.compile("\\\\u([0-9a-fA-F]{4})").matcher(str)
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toInt(16).toChar().toString() + "")
        }
        matcher.appendTail(sb)
        return sb.toString().replace("\\", "")
    }

    /**
     * 检查内容是否包含emoji表情
     */
    fun containsEmoji(content: String?): Boolean {
        val p = Pattern.compile(
            "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
            Pattern.UNICODE_CASE or Pattern.CASE_INSENSITIVE
        )
        val m = p.matcher(content)
        return m.find()
    }

    /**
     * 检查是否包含文字和字母
     */
    fun isContainLetterAndDigit(str: String): Boolean {
        var isDigit = false
        var isLetter = false
        for (i in 0 until str.length) {
            if (Character.isDigit(str[i])) {
                isDigit = true
            } else if (Character.isLetter(str[i])) {
                isLetter = true
            }
        }
        val regex = "^[a-zA-Z0-9]+$"
        return isDigit && isLetter && str.matches(regex.toRegex())
    }

    /**
     * 判断是否是车牌号
     */
    fun isCarNo(carNo: String?): Boolean {
        val pattern =
            Pattern.compile("^[\u4e00-\u9fa5]{1}[a-hj-zA-HJ-Z]{1}[a-hj-zA-HJ-Z_0-9]{4}[a-hj-zA-HJ-Z_0-9_\u4e00-\u9fa5]$")
        val matcher = pattern.matcher(carNo)
        return matcher.matches()
    }

    /**
     * 判断是否包含某个字符串
     */
    fun contains(str: String, title: String?): Boolean {
        return if (TextUtils.isEmpty(str) || TextUtils.isEmpty(title)) {
            false
        } else str.contains(title!!)
    }

}